import axios from 'axios';
//登录接口
export const URI_LOGIN = "/security/api/login";
//初始化链接
export const URI_INIT = "/security/api/index/init";
//登出系统
export const URI_LOGOUT = "/security/api/logout";
//判断是否有角色
const URI_HAS_ROLES = "/security/api/roles";	
//判断是否有权限
const URI_HAS_PERMS = "/security/api/perms";
//获取数据字典信息	
const URI_DICT = "/security/api/dict";
//批量获取数据字典信息
const URI_DICTS = "/security/api/dicts";	
//设置用户配置
const URI_SET_SETTING = "/security/api/index/setting";
//获取单个用户配置
const URI_SINGLE_SETTING = "/security/api/index/singlesetting";
//获取用户所有配置
const URI_SETTINGS = "/security/api/index/settings";
//下载地址
export const URI_DOWNLOAD = "/filemanager/api/filemanager/file/download";

export const getInitData = (menuType, callback = function(res) {}) => {
	axios.get(URI_INIT, {
        params: {
        	menuType
        }
    }).then((response) => {
    	callback(response.data);
    	
    });
}

export const logout = (callback = function(res) {}) => {
	axios.get(URI_LOGOUT).then((response) => {
		callback(response.data);
	});
}

export const login = (params, callback = function(res) {}) => {
	axios.post(URI_LOGIN, params).then((response) => {
		callback(response.data);
	});
}

export const hasRoles = (roles, callback = function(res) {}) => {
	axios.post(URI_HAS_ROLES, roles).then((response) => {
		callback(response.data);
	});
}

export const hasPerms = (perms, callback = function(res) {}) => {
	axios.post(URI_HAS_PERMS, perms).then((response) => {
		callback(response.data);
	});
}

export const getSimpleDict = (params, callback = function(res) {}) => {
	axios.post(URI_DICT, params).then((response) => {
		callback(response.data);
	});
}

export const getMultiDicts = (params, callback = function(res) {}) => {
	const options = {
		headers: { 'Content-Type': "application/json"},
	};
	axios.post(URI_DICTS, params, options).then((response) => {
		callback(response.data);
	});
}

export const setUserSetting = (key, value) => {
	axios.post(URI_SET_SETTING, {key, value}).then((response) => {});
}

export const getUserSetting = (key, callback = function(value) {}) => {
	axios.get(URI_SINGLE_SETTING, { params: {key} }).then((response) => {
		const res = response.data;
		if(res.success) {
			callback(res.data);
		} else {
			callback('');
		}
	});
} 

export const getUserSettings = (callback = function(settings) {}) => {
	axios.get(URI_SETTINGS).then((response) => {
		const res = response.data;
		if(res.success) {
			callback(res.data);
		} else {
			callback({});
		}
	});
}

export default {
	getInitData,
	logout,
	login,
	hasRoles,
	hasPerms,
	getSimpleDict,
	getMultiDicts,
	setUserSetting,
	getUserSetting,
	getUserSettings
}